#include "mainwindow.h"
#include "ui_mainwindow.h"

QMap<QString,intptr_t>* MainWindow::db=new QMap<QString,intptr_t>();
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setFixedSize(1000,600);
    //设置图标
    setWindowIcon(QIcon(":/stand.png"));

    //设置标题
    setWindowTitle("拳击小游戏");

    eventLoop=new QTimer();
    view=new QGraphicsView(ui->centralwidget);
    view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    view->setSceneRect(QRect(QPoint(0,0), size()));
    view->setGeometry(QRect(QPoint(0,0), size()));   //使view的大小布满整个窗口
    connect(eventLoop,&QTimer::timeout,this,&MainWindow::run);
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    combatScene=new CombatScene();
    menuScene=new MenuScene();
    overScene=new OverScene();


    QPixmap background(":/bg_0.jpeg");
    background.scaled(1000,600);
    QGraphicsPixmapItem *item = combatScene->addPixmap(background);
    item->setZValue(-1);

    QPixmap menuBackground(":/menubg.jpg");
    menuBackground.scaled(1000,600,Qt::IgnoreAspectRatio);
    QGraphicsPixmapItem * menuitem = menuScene->addPixmap(menuBackground);
    menuitem->setZValue(-1);

    QPixmap overBackground(":/bg_0.jpeg");
    overBackground.scaled(1000,600,Qt::IgnoreAspectRatio);
    QGraphicsPixmapItem * overitem = overScene->addPixmap(overBackground);
    overitem->setZValue(-1);

    //set方法设置当前场景,初始化的场景就为 菜单 界面
    setScene(menuScene);
    view->setFrameStyle(QFrame::NoFrame);


}

MainWindow::~MainWindow()
{
    delete ui;
    delete view;
    delete eventLoop;
    delete combatScene;
}

void MainWindow::startLoop(){
    eventLoop->start(1000/60);
    qDebug()<<"mainwindow running";
}

void MainWindow::stopLoop(){
    eventLoop->stop();
}

void MainWindow::run(){
   currentScene->run();
   view->update();
//   qDebug()<<this->x()<<","<<this->y()<<","<<this->width()<<","<<this->height()<<endl;

}
void MainWindow::setScene(GameScene *scene){
    scene->setView(view);
    view->setScene(scene);
    currentScene=scene;
    scene->init();

}
void MainWindow::toCombatScene(){
    setScene(combatScene);
}

int MainWindow::scoreP1=0;
int MainWindow::scoreP2=0;
